package com.unittec.drainage.sdk.root.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: drainage-merger-parent
 * @description:
 * @author: djj
 * @create: 2019-06-29 16:44
 **/

public class DateUtiles {
    /**
     * 获取现在时间
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 指定日期 + 指定小时
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date plusDate(Date date, Integer hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        date = cal.getTime();
        return date;
    }

    /**
     * 指定日期 + 指定年数
     *
     * @param date
     * @param years
     * @return
     */
    public static Date plusYearDate(Date date, Integer years) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, years);
        date = cal.getTime();
        return date;
    }

    /**
     * 指定日期 + 指定天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date plusDayDate(Date date, Integer days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        date = cal.getTime();
        return date;
    }

    /**
     * 日期 - 1
     *
     * @param date
     * @return
     */
    public static String getBeforeDayDate(Date date, Integer dayNumber) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - dayNumber);
        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    /***
     * 按格式yyyy-MM-dd HH:mm:ss
     *
     * @param dateStr
     * @return
     */
    public static Date formatDate(String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return formatter.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定日期当天起始yyyy-MM-dd 00:00:00
     *
     * @param searchDate
     * @return
     */
    public static Date getDayStart(Date searchDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(searchDate);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        return c.getTime();
    }

    /**
     * 获取指定日期的当天截止yyyy-MM-dd 00:00:00
     *
     * @param searchDate
     * @return
     */
    public static Date getDayEnd(Date searchDate) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(searchDate);
        ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH), ca.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        return ca.getTime();
    }

    /**
     * 获取指定日期的月第一天yyyy-MM-dd 00:00:00
     *
     * @param searchDate
     * @return
     */
    public static Date getMonthStart(Date searchDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(searchDate);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }

    /**
     * 获取指定日期的月最后一天yyyy-MM-dd 00:00:00
     *
     * @param searchDate
     * @return
     */
    public static Date getMonthEnd(Date searchDate) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(searchDate);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return ca.getTime();
    }

    /**
     * @param date
     * @return
     */
    public static String format(Date date, String format) {
        if (null == format) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static String getDateStr() {
        Date date = getNowDate();
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * yyyy-MM-dd HH:mm:ss 转 yyyyMMddHHmmss
     *
     * @param
     * @return
     */
    public static String getDateStr2(String reqDate) {
        Date date = formatDate(reqDate);
        return format(date, "yyyyMMddHHmmss");
    }

    /**
     * 当前时间 转 yyyyMMddHHmmss
     *
     * @param
     * @return
     */
    public static String getDateStr3() {
        return getDateStr2(getDateStr());
    }

    /**
     * yyyy-MM-dd 格式比较大小 data > date2 false else true
     */
    public static Boolean compareTime(Date date, String date2) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = dateFormat.format(date);
        // date1与date2进行比较
        String[] d1 = date1.split("-");
        String[] d2 = date2.split("-");
        Integer year1 = new Integer(d1[0]);
        Integer year2 = new Integer(d2[0]);
        if (year1 > year2) {
            return false;
        } else if (year1.equals(year2)) {
            Integer month1 = new Integer(d1[1]);
            Integer month2 = new Integer(d2[1]);
            if (month1 > month2) {
                return false;
            } else if (month1.equals(month2)) {
                Integer day1 = new Integer(d1[2]);
                Integer day2 = new Integer(d2[2]);
                return day1 <= day2;
            }
        }
        return true;
    }

    /**
     * yyyy-MM-dd 格式比较大小 data > 当前时间 false else true
     */
    public static Boolean compareNow(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = dateFormat.format(date);
        String date2 = dateFormat.format(new Date());
        // date1与date2进行比较
        String[] d1 = date1.split("-");
        String[] d2 = date2.split("-");
        Integer year1 = new Integer(d1[0]);
        Integer year2 = new Integer(d2[0]);
        if (year1 > year2) {
            return false;
        } else if (year1.equals(year2)) {
            Integer month1 = new Integer(d1[1]);
            Integer month2 = new Integer(d2[1]);
            if (month1 > month2) {
                return false;
            } else if (month1.equals(month2)) {
                Integer day1 = new Integer(d1[2]);
                Integer day2 = new Integer(d2[2]);
                return day1 < day2;
            }
        }
        return true;
    }

    /**
     * yyyy-MM-dd 格式比较大小 data = 当前时间 true else false
     */
    public static Boolean compareDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = dateFormat.format(date);
        String date2 = dateFormat.format(new Date());
        // date1与date2进行比较
        String[] d1 = date1.split("-");
        String[] d2 = date2.split("-");
        Integer year1 = new Integer(d1[0]);
        Integer year2 = new Integer(d2[0]);
        if (!year1.equals(year2)) {
            return false;
        } else {
            Integer month1 = new Integer(d1[1]);
            Integer month2 = new Integer(d2[1]);
            if (!month1.equals(month2)) {
                return false;
            } else {
                Integer day1 = new Integer(d1[2]);
                Integer day2 = new Integer(d2[2]);
                return day1.equals(day2);
            }
        }
    }

    /**
     * 时间戳转时间
     *
     * @param unixDate
     * @return
     */
    public static String stampToDate(String unixDate) {
        SimpleDateFormat fm1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long unixLong = 0;
        String date = "";
        try {
            unixLong = Long.parseLong(unixDate) * 1000;
        } catch (Exception ex) {
            System.out.println("String转换Long错误，请确认数据可以转换！");
        }
        try {
            date = fm1.format(unixLong);
            date = fm2.format(new Date(date));
        } catch (Exception ex) {
            System.out.println("String转换Date错误，请确认数据可以转换！");
        }
        return date;
    }

    /**
     * 判断时间是否大于指定时间
     *
     * @param nowTime
     * @param startTime
     * @return
     */
    public static boolean judgeGreaterDate(Date nowTime, Date startTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        return date.after(begin);
    }

    /**
     * 日期 + min
     *
     * @param date
     * @return
     */
    public static Date getBeforeDayTime(Date date, Integer minNumber) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, minNumber);
        return nowTime.getTime();
    }

    /**
     * 判断时间是否小于当前时间-10min
     *
     * @return true 时间小于当前时间-10min
     */
    public static boolean tenMinuteBefore(Date time) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar begin = Calendar.getInstance();
        Date beforeDayTime = getBeforeDayTime(new Date(), -10);
        begin.setTime(beforeDayTime);
        return date.before(begin);
    }

}
